<?php

namespace App\Admin\Controllers;

use App\Admin\Show\Capitals;
use App\Models\XxCapitalFlow;
use App\Models\XxMerchantShop;
use App\Models\XxMerchantUser;
use App\Models\XxPushTask;
use App\Models\XxPushUser;
use Encore\Admin\Widgets\Table;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class PushTaskController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'XxPushTask';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        $grid = new Grid(new XxPushTask());

        $grid->disableActions();
        $grid->disableBatchActions();
        $grid->disableCreateButton();
        $grid->disableRowSelector();


        $grid->model()->whereBetween('create_time', [1619625601, 1619798399]);
        $grid->model()->where('task_type', 1);
        $grid->model()->orderBy('task_code', 'desc');

        $grid->setTitle("派单数据");

        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            $filter->equal('task_code', '任务编号');
            $filter->equal('order_num', '订单号');
            $filter->equal('merchant_id', '商户ID');
            $filter->equal('push_id', '刷手ID');
        });

        $grid->column('task_id', __('任务ID'))->display(function () {
            return "[{$this->task_id}]资金流水";
        })->modal("资金流水", Capitals::class);
        $grid->column('order_num', __('订单号'));
        $grid->column('merchant_id', __('商户账号'))->display(function () {
            return "[{$this->merchant_id}]".XxMerchantUser::where('id', $this->merchant_id)->value('phone');
        }); // 商户账号
        $grid->column('push_id', __('刷手账号'))->display(function () {
            return "[{$this->push_id}]".XxPushUser::where('id', $this->push_id)->value('phone');
        }); // 刷手账号
        $grid->column('shop_id', __('店铺名称'))->display(function () {
            return XxMerchantShop::where('id', $this->shop_id)->value('name');
        }); // 店铺名称
        $grid->column('goods_id', __('商品ID'));
        $grid->column('keyword', __('关键词'));
        // 平台类型 1:淘宝 2：天猫 3：京东 13：唯品会 15：拼多多
        $grid->column('shop_type', __('平台类型'))->using([
            1 => '淘宝', 2 => '天猫', 3 => '京东', 13 => '唯品会', 15 => '拼多多'
        ]);
        $grid->column('commission', __('推手佣金'));
        $grid->column('tb_name', __('淘宝账号'));
        $grid->column('pay_money', __('订单金额'));
        //用户做单状态 ：1待领取  2待领取超时 3领取后用户放弃  4操作中 5操作超时 6待审核
        //  7完成任务 8审核失败 9平台终止 10商家终止
        $grid->column('status', __('订单状态'))->using([
            1 => '待领取', 2 => '待领取超时', 3 => '领取后用户放弃', 4 => '操作中', 5 => '操作超时',
            6 => '待审核', 7 => '完成任务', 8 => '审核失败', 9 => '平台终止', 10 => '商家终止'
        ])->filter([
            1 => '待领取', 2 => '待领取超时', 3 => '领取后用户放弃', 4 => '操作中', 5 => '操作超时',
            6 => '待审核', 7 => '完成任务', 8 => '审核失败', 9 => '平台终止', 10 => '商家终止'
        ]);
        //审核状态 1未审核、2审核成功、3审核失败、4待人工审核
        $grid->column('check_status', __('审核状态'))->using([
            1 => '未审核', 2 => '审核成功', 3 => '审核失败', 4 => '待人工审核'
        ])->filter([
            1 => '未审核', 2 => '审核成功', 3 => '审核失败', 4 => '待人工审核'
        ]);
        $grid->column('mark', __('Mark'));
        $grid->column('settlement_status', __('结算状态'))->using([
            1 => '未结算', 2 => '已结算'
        ]);
        $grid->column('remark', __('审核失败原因'));
        $grid->column('real_pay', __('实际金额'));
        $grid->column('other_pay', __('其他平台实际支付金额'));
        $grid->column('give_up_message', __('放弃原因'));
        $grid->column('buy_num', __('购买数量'));
        $grid->column('plan_code', __('计划编号'));
        $grid->column('task_code', __('任务编号'));
        $grid->column('receive_time', __('领取时间'))->display(function () {
            return date('Y-m-d H:i:s', $this->receive_time);
        });
        $grid->column('over_time', __('任务提交时间'))->display(function () {
            return date('Y-m-d H:i:s', $this->over_time);
        });
        $grid->column('check_time', __('审核时间'))->display(function () {
            return date('Y-m-d H:i:s', $this->check_time);
        });
        $grid->column('check_over_time', __('审核截止时间'))->display(function () {
            return date('Y-m-d H:i:s', $this->check_over_time);
        });
        $grid->column('buy_time', __('下单时间'))->display(function () {
            return date('Y-m-d H:i:s', $this->buy_time);
        });
        $grid->column('create_time', __('派单时间'))->display(function () {
            return date('Y-m-d H:i:s', $this->create_time);
        });
        $grid->column('rebate_type', __('返款类型 '))->using([
            1 => '商家返款', 2 => '普通返款'
        ]);

        $grid->perPages([10, 20, 50, 100, 200]);
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(XxPushTask::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('province', __('Province'));
        $show->field('city', __('City'));
        $show->field('id_card_address', __('Id card address'));
        $show->field('ip', __('Ip'));
        $show->field('order_num', __('Order num'));
        $show->field('merchant_id', __('Merchant id'));
        $show->field('plan_id', __('Plan id'));
        $show->field('task_id', __('Task id'));
        $show->field('push_id', __('Push id'));
        $show->field('shop_id', __('Shop id'));
        $show->field('goods_id', __('Goods id'));
        $show->field('keyword', __('Keyword'));
        $show->field('shop_type', __('Shop type'));
        $show->field('commission', __('Commission'));
        $show->field('tb_name', __('Tb name'));
        $show->field('pay_money', __('Pay money'));
        $show->field('status', __('Status'));
        $show->field('check_status', __('Check status'));
        $show->field('mark', __('Mark'));
        $show->field('settlement_status', __('Settlement status'));
        $show->field('task_type', __('Task type'));
        $show->field('remark', __('Remark'));
        $show->field('flow_task_type', __('Flow task type'));
        $show->field('real_pay', __('Real pay'));
        $show->field('other_pay', __('Other pay'));
        $show->field('pay_img', __('Pay img'));
        $show->field('give_up_message', __('Give up message'));
        $show->field('buy_num', __('Buy num'));
        $show->field('plan_code', __('Plan code'));
        $show->field('task_code', __('Task code'));
        $show->field('receive_time', __('Receive time'));
        $show->field('over_time', __('Over time'));
        $show->field('check_time', __('Check time'));
        $show->field('check_over_time', __('Check over time'));
        $show->field('buy_time', __('Buy time'));
        $show->field('create_time', __('Create time'));
        $show->field('rebate_type', __('Rebate type'));
        $show->field('flag_status', __('Flag status'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new XxPushTask());

        $form->text('province', __('Province'));
        $form->text('city', __('City'));
        $form->text('id_card_address', __('Id card address'));
        $form->ip('ip', __('Ip'));
        $form->text('order_num', __('Order num'));
        $form->number('merchant_id', __('Merchant id'));
        $form->number('plan_id', __('Plan id'));
        $form->number('task_id', __('Task id'));
        $form->number('push_id', __('Push id'));
        $form->number('shop_id', __('Shop id'));
        $form->number('goods_id', __('Goods id'));
        $form->text('keyword', __('Keyword'));
        $form->number('shop_type', __('Shop type'))->default(1);
        $form->decimal('commission', __('Commission'))->default(0.00);
        $form->text('tb_name', __('Tb name'));
        $form->decimal('pay_money', __('Pay money'))->default(0.00);
        $form->number('status', __('Status'))->default(1);
        $form->switch('check_status', __('Check status'))->default(1);
        $form->switch('mark', __('Mark'))->default(2);
        $form->switch('settlement_status', __('Settlement status'))->default(1);
        $form->switch('task_type', __('Task type'))->default(1);
        $form->text('remark', __('Remark'));
        $form->number('flow_task_type', __('Flow task type'));
        $form->decimal('real_pay', __('Real pay'))->default(0.00);
        $form->decimal('other_pay', __('Other pay'))->default(0.00);
        $form->text('pay_img', __('Pay img'));
        $form->text('give_up_message', __('Give up message'));
        $form->number('buy_num', __('Buy num'));
        $form->text('plan_code', __('Plan code'));
        $form->text('task_code', __('Task code'));
        $form->number('receive_time', __('Receive time'));
        $form->number('over_time', __('Over time'));
        $form->number('check_time', __('Check time'));
        $form->number('check_over_time', __('Check over time'));
        $form->number('buy_time', __('Buy time'));
        $form->number('create_time', __('Create time'));
        $form->switch('rebate_type', __('Rebate type'))->default(2);
        $form->switch('flag_status', __('Flag status'))->default(2);

        return $form;
    }
}
